//
//  SYJMoreProductView.m
//  JieJieKan
//
//  Created by 极速现金侠 on 2017/7/6.
//  Copyright © 2017年 innext. All rights reserved.
//

#import "SYJMoreProductView.h"
#import "SYJMoreProductCollectionViewCell.h"
//#import "SYJTradeListMode.h"

#define collectionHeight  82*AutoSizeScaleYY

@interface SYJMoreProductView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic , weak) UILabel *moreProductLab;

@property (nonatomic , strong) UICollectionView *collection;
@property (nonatomic , strong) NSArray *productArray;

@end

@implementation SYJMoreProductView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self configFrame];
}
- (void)configLoanRecodeMoreProduct:(NSArray *)productArray{
    self.productArray = productArray;
    
    if ([JSLKitFactory isNotNull:self.productArray]) {
        [self.collection reloadData];
    }
    
}
#pragma mark - collectionView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.productArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SYJMoreProductCollectionViewCell *cell = (SYJMoreProductCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"MoreProductCollectionViewCel" forIndexPath:indexPath];
    [cell configMoreProduc:self.productArray[indexPath.row]];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.moreProductViewDelegate respondsToSelector:@selector(tapMoreProduct:info:)]) {
        [self.moreProductViewDelegate tapMoreProduct:self info:self.productArray[indexPath.row]];
    }
}

// 和UITableView类似，UICollectionView也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView *headerView = [self.collection dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        if(headerView == nil)
        {
            headerView = [[UICollectionReusableView alloc] init];
        }
        headerView.backgroundColor = [@"#eaecf2" hexStringToColor];
        
        return headerView;
    }
    
//    if([kind isEqualToString:UICollectionElementKindSectionFooter])
    else
    {
        UICollectionReusableView *footerView = [self.collection dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        if(footerView == nil)
        {
            footerView = [[UICollectionReusableView alloc] init];
        }
        footerView.backgroundColor = [@"#eaecf2" hexStringToColor];
        
        return footerView;
    }
    
   
//    return [JSLKitFactory createViewWithFrame:CGRectMake(0, 0, 20, 110*AutoSizeScaleYY -20*AutoSizeScaleYY) backgroundColor:[UIColor greenColor] superView:nil];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return (CGSize){10*AutoSizeScaleXX,collectionHeight};
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return (CGSize){10*AutoSizeScaleXX,collectionHeight};
}

#pragma mark - 配置视图
- (void)configView{
    
    self.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    float imageVwidth = 150*AutoSizeScaleXX;
    layout.itemSize = CGSizeMake(imageVwidth,collectionHeight);
    layout.minimumInteritemSpacing = 2*AutoSizeScaleXX;
    layout.minimumLineSpacing = 10*AutoSizeScaleXX;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collection =[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collection.backgroundColor = [@"#eaecf2" hexStringToColor];
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.showsHorizontalScrollIndicator = NO;
    
    _collection.alwaysBounceHorizontal = NO;
    [_collection registerClass:[SYJMoreProductCollectionViewCell class] forCellWithReuseIdentifier:@"MoreProductCollectionViewCel"];

    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [self addSubview:_collection];
}

#pragma mark - 配置坐标
- (void)configFrame{
    

    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.width.equalTo(self);
        make.height.equalTo(@(collectionHeight));
    }];
}


@end
